#include "returndialog.h"
#include "ui_returndialog.h"

ReturnDialog::ReturnDialog(User_t user, QWidget *parent) :
    QDialog(parent),user(user),
    ui(new Ui::ReturnDialog)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");

    move(100, 40);
    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
    ui->listWidget->setFrameShape(QListWidget::NoFrame);
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setFocusPolicy(Qt::NoFocus);
    ui->listWidget->setViewMode(QListWidget::ListMode);
    ui->listWidget->setGridSize(QSize(600, 80));
    ui->listWidget->setFlow(QListWidget::TopToBottom);
    QScroller::grabGesture(ui->listWidget, QScroller::TouchGesture);
    //connect(ui->listWidget, &QListWidget::itemClicked, this, &User::itemClicked);

    QList<Lend_t> list = Sqlite::Get()->Select_lend(user.id);
    foreach(Lend_t book, list)
    {
        //qDebug() <<
        listWidgetAddItem(book.book, book.sell);
    }

    if(list.size() <= 0)
    {
        QString data = "您暂未借阅任何图书，无需归还！";
        QLabel *label = new QLabel(data, this);
        label->setGeometry(0,0,600,300);
        label->setWordWrap(true);
        label->setAlignment(Qt::AlignCenter);
        label->setStyleSheet("font-size:30px;");
        ui->listWidget->hide();
    }
    else
    {
        ui->listWidget->setCurrentRow(0);
    }
}

ReturnDialog::~ReturnDialog()
{
    delete ui;
}

void ReturnDialog::listWidgetAddItem(QString book, int sell)
{
    QListWidgetItem *item = new QListWidgetItem(ui->listWidget);
    item->setData(NameRole, book);
    item->setData(SellRole, sell);
    item->setSizeHint(QSize(600, 80));
    QSize size = item->sizeHint();

    QWidget *widget = new QWidget();
    widget->setFixedSize(size);

    QLabel *icon_label = new QLabel(book, widget);
    icon_label->setFixedSize(300, 80);
    icon_label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    icon_label->setStyleSheet("color:#000;font-size:30px;");
    icon_label->move(0,0);
    //icon_label->setStyleSheet(icon);

    QPushButton *btn = new QPushButton("归还", widget);
    btn->setFixedSize(90, 80);
    btn->setStyleSheet("QPushButton{color:#fff;font-size:30px;}");
    btn->setProperty("name", book);
    btn->setProperty("sell", sell);
    connect(btn, &QPushButton::clicked, this, &ReturnDialog::sendBtn_click);
    btn->move(500, 0);

    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item, widget);
}

void ReturnDialog::on_pushButton_clicked()
{
    this->close();
}

void ReturnDialog::sendBtn_click()
{
    QPushButton *btn = static_cast<QPushButton *>(sender());
    if(btn)
    {
        QString name = btn->property("name").toString();
        int sell = btn->property("sell").toInt();

        user.money += sell;
        Sqlite::Get()->Update_userMoney(user);
        //Sqlite::Get()->Delete_lend(user.id, name);
        Sqlite::Get()->Update_bookNum(name);

        for(int i=0; i<ui->listWidget->count(); i++)
        {
            QListWidgetItem *item = ui->listWidget->item(i);
            if(item && item->data(NameRole).toString() == name)
            {
                ui->listWidget->removeItemWidget(item);
                delete item;
                item = NULL;
            }
        }
        emit sigMoney(user.money);

        QString data = QString("归还图书《%1》成功").arg(name);
        QMessageBox::about(this, "tips", data);
        qDebug() << name << " " << sell;

        if(ui->listWidget->count() <= 0)
        {
            QString data = "您暂未借阅任何图书，无需归还！";
            QLabel *label = new QLabel(data, this);
            label->setGeometry(0,0,600,300);
            label->setWordWrap(true);
            label->setAlignment(Qt::AlignCenter);
            label->setStyleSheet("font-size:30px;");
            label->show();
            ui->listWidget->hide();
        }
    }
}
